//
//  profileViewController.m
//  twittsTab2
//
//  Created by ccg on 4/8/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "profileViewController.h"
#import "TwitterRequest.h"


@implementation profileViewController

@synthesize listData;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	TwitterRequest *friends = [[TwitterRequest alloc] init];
	
	/*if (standardUserDefaults){
	 username = [standardUserDefaults objectForKey:@"username"];
	 password = [standardUserDefaults objectForKey:@"password"];
	 }*/
	
	friends.username = @"CHCProject";
	friends.password = @"team22";
	stories = [[NSMutableArray alloc] init];
	dates = [[NSMutableArray alloc] init];
	
	NSLog(@"Getting timeline for user %@", friends.username);
	
	[friends friends_timeline:self requestSelector:@selector(friends_timeline_callback:)];
	
	
    [super viewDidLoad];
}

- (void)friends_timeline_callback:(NSData *)data{
	//NSString *timeline = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	
	timelineXML = [[NSXMLParser alloc] initWithData:data];
	
	[timelineXML setDelegate:self];
	BOOL success = [timelineXML parse];
	
    if(success)
		NSLog(@"no errors");
	else
		NSLog(@"Errors Errors");
	
	[self moodTable];
	//NSLog(@"%@",stories);
	//NSLog(@"%@",dates);
	//[tbl reloadData];
	//NSLog(@"Timeline %@", timeline);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
	attributes:(NSDictionary *)attributeDict {
	currentElement = [elementName copy];
	currentElement2 = [elementName copy];
	if([elementName isEqualToString:@"status"]){
		item = [[NSMutableDictionary alloc] init];
		currentTitle = [[NSMutableString alloc] init];
		item2 = [[NSMutableDictionary alloc] init];
		currentTitle2 = [[NSMutableString alloc] init];

	}
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	if([currentElement isEqualToString:@"text"]){
		[currentTitle appendString:string];
	}
	if([currentElement2 isEqualToString:@"created_at"]){
		[currentTitle2 appendString:string];
	}
	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	if([elementName isEqualToString:@"status"])
	{
		[item setObject:currentTitle forKey:@"text"];
		[stories addObject:[item objectForKey:@"text"]];
		
		[item2 setObject:currentTitle2 forKey:@"created_at"];
		[dates addObject:[item2 objectForKey:@"created_at"]];
		//NSLog(@"adding status: %@", currentTitle);
		//NSLog(@"%@",stories);
	}
	
}

-(void)moodTable{
	NSString *temp;
	NSInteger dayAfter;
	NSString *temp2;
	NSString *tempDate;
	
	int count= 0;
	/*
	NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
	NSDate *date = [NSDate date];
	//NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];*/
	
	//NSInteger year = [dateComponents year];
	//NSInteger month = [dateComponents month];
	//NSInteger day = [dateComponents day];
	
	
	for(int i = 0; i < 20; i++){
		if(count >= 3){
			break;
		}
		temp = [stories objectAtIndex:i];
		temp2 = [temp substringWithRange:NSMakeRange(0, 2)];
		//NSLog(@"%@", temp2);
		if([temp2 isEqualToString:@"m$"]){
			tempDate = [dates objectAtIndex:i];
			NSLog(@"status:%@ ; time: %@", temp, tempDate);
			count++;
		}
	}

	
	
	
	
	//[calendar release];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
